/* *********************************************
as.TreeShaker
************************************************

Description:	Screen full of particles

Creation date:	June 8, 2012
Created by:	Kirby Szeto (Plus K Studios)
Modified:	-
Notes:	-

***********************************************/

import flash.display.*;
import flash.filters.DisplacementMapFilter;
import flash.geom.Point;

class as.TreeShaker extends MovieClip {
	// class definition
	
	
	// variables
	var dmap:BitmapData;

	var mapPoint:Point;
	var cX:Number = 1;
	var cY:Number = 2;
	var sX:Number = 10;
	var sY:Number = 0;
	
	private var peaches:Array;
	
	
	var _xm = 0;
	var _xx = 0;
	var isDown = false;
	var _px = 0;
	
	var shakemap_mc;
	var tree_mc;
	var peaches_mc;
	var hiders_mc;
	
	var shakeCallBack;
	
	public function TreeShaker () {
		mapPoint = new Point( 0, 0 );
		
		dmap = new BitmapData( 199, 379, false );
		dmap.draw( shakemap_mc );
		shakemap_mc._visible = false;
		
		initPeaches();
		
		startShake();
	}
	
	private function initPeaches() {
		peaches = [];
		for (var e in peaches_mc) {
			if (typeof peaches_mc[e] == "movieclip") {
				var p = peaches_mc[e];
				p.shadow_mc._visible = false;
				p._size = p._width / 40;
				p._ox = p._x;
				p._or = p._rotation;
				
				p._resist = Math.random() * 10;
				
				peaches.push( p );
			}
		}
		peaches[2]._resist = .5;
		peaches[15]._resist = .8;
		peaches[6]._resist = 1;
		peaches[8]._resist = 2;
	}
	
	public function startShake() {
		tree_mc.onPress = function() {
			trace ( this._parent + " : " + this._parent.isDown);
			this._parent.isDown = true;
			//this._parent._px = this._parent._xmouse;
			
		}
		tree_mc.onMouseOut = tree_mc.onRelease = tree_mc.onReleaseOutside = function() {
			this._parent.isDown = false;
			this._parent.shakeCallBack();
		}
		onEnterFrame = update;
	}
	
	private function update() {
		var _nm = 0;
		//if (isDown) {
		
			var nx = _xmouse;
			var mouseVelocity = nx - _px;
			var lim = 200; // isDown ? 200 : 50;
			mouseVelocity = Math.min( lim, mouseVelocity );
			mouseVelocity = Math.max( -lim, mouseVelocity );
			var denom = isDown ? 100 : 200;
			_nm = mouseVelocity / denom;
			_px = nx;
		//}
		
		
		
		
		_xm += (_nm - _xx) * .5;
		
		_xx += _xm;
		_xm *= .5;
		
		
		//var dfilt = new DisplacementMapFilter( dmap, mapPoint, cX, cY, sX*Math.sin(this._xv), sY );
		var dfilt = new DisplacementMapFilter( dmap, mapPoint, cX, cY, sX*_xx, sY );
		tree_mc.filters = [ dfilt ];
		hiders_mc.filters = [ dfilt ];
		
		for (var i = 0; i < peaches.length; i++) {
			var p = peaches[i];
			p._xv = this._xx;
			p._x = p._ox + ( sX * this._xx * p._size * .5 );
			p._rotation = p._or - ( sX * this._xx );
			
			p._resist -= Math.abs( _xm );
			if (isDown) p._resist -= Math.abs( _xm );
			
			if (p._resist <= 0) {
				
				dropPeach( peaches.splice( i, 1 )[0] );
				
				//if (peaches.length == 0) {
					//shakeCallBack();
				//}
			}
		}
		
		
		// peaches_mc.filters = [ dfilt ];

		
	}
	
	public function forceShake() {
		_xm += .2;
	}
	
	private function dropPeach( p ) {
		p.filters = [];
		p._yvel = 0;
		p._ground = p._size * 100;
		p._xv *= 1.5;
		p._rv = p._xv * 2;
		p._bounced = false;
		p.onEnterFrame = function() {
			
			this._y += this._yvel;
			this._yvel += this._size;
			
			this._x += this._xv;
			this._rotation += this._rv;
			
			if (this._y > this._ground) {
				if (!this._bounced) {
					this._y = this._ground - (this._y-this._ground);
					this._yvel *= -.1;
					this._xv *= 1.3;
					this._rv *= 2;
				} else {
					this.shadow_mc._visible = true;
					this.shadow_mc._rotation = -this._rotation;
					this._yvel = 0;
					this._xv *= .8;
					if ( Math.abs( this._xv ) < .1 ) {
						delete this.onEnterFrame;
					}
				}
				this._bounced = true;
				
			}
		}
	}
	
}